package com.deercrowing.takeaway.service.impl;

import com.deercrowing.takeaway.common.constant.OrderStatusConstant;
import com.deercrowing.takeaway.common.constant.StatusConstant;
import com.deercrowing.takeaway.dao.DishesMapper;
import com.deercrowing.takeaway.dao.OrdersMapper;
import com.deercrowing.takeaway.dao.SetmealsMapper;
import com.deercrowing.takeaway.dao.UsersMapper;
import com.deercrowing.takeaway.service.WorkSpaceService;
import com.deercrowing.takeaway.vo.BusinessDataVO;
import com.deercrowing.takeaway.vo.DishViewVO;
import com.deercrowing.takeaway.vo.OrderViewVO;
import com.deercrowing.takeaway.vo.SetmealViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WorkSpaceServicelmpl implements WorkSpaceService {

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    DishesMapper dishesMapper;

    @Autowired
    SetmealsMapper setmealsMapper;

    @Autowired
    UsersMapper usersMapper;

    @Override
    public BusinessDataVO businessDataManage(LocalDateTime begin, LocalDateTime end) {
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("begin",begin);
        dataMap.put("end",end);

        //查询总订单数
        Integer totalOrderCount = ordersMapper.countByMap(dataMap);

        dataMap.put("status", OrderStatusConstant.COMPLETED);
        //营业额
        Double turnover = ordersMapper.sumByMap(dataMap);
        turnover = turnover == null? 0.0 : turnover;

        //有效订单数
        Integer validOrderCount = ordersMapper.countByMap(dataMap);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        Integer newUsers = usersMapper.countByMap(dataMap);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers).build();
    }

    @Override
    public OrderViewVO OrderViewManage() {
        Map<String,Object> map = new HashMap<>();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", OrderStatusConstant.NOT_ACCEPTED_ORDER);

        //待接单
        Integer waitingOrders = ordersMapper.countByMap(map) == 0 ? 0:ordersMapper.countByMap(map);

        //待派送
        map.put("status", OrderStatusConstant.RECEIVED_ORDER);
        Integer deliveredOrders = ordersMapper.countByMap(map) == 0 ? 0:ordersMapper.countByMap(map);

        //已完成
        map.put("status", OrderStatusConstant.COMPLETED);
        Integer completedOrders = ordersMapper.countByMap(map) == 0 ? 0:ordersMapper.countByMap(map);

        //已取消
        map.put("status", OrderStatusConstant.CANCEL);
        Integer cancelledOrders = ordersMapper.countByMap(map) == 0 ? 0:ordersMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = ordersMapper.countByMap(map) == 0 ? 0:ordersMapper.countByMap(map);

        return OrderViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    public DishViewVO DishViewManage() {
        Map<String,Object> map = new HashMap<>();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishesMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishesMapper.countByMap(map);

        return DishViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    public SetmealViewVO SetmealViewManage() {
        Map<String,Object> map = new HashMap<>();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealsMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealsMapper.countByMap(map);

        return SetmealViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
